﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Applications.Core.Common;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using Pcm.Retail.Client;
using System.Windows.Forms;
using CdcSoftware.Pivotal.Engine.UI.Forms;

namespace Pcm.Retail.Client.FormTasks
{
    public partial class RE_Promotion : FormClientTask
    {
        string previousType = String.Empty;
        bool checkUpdate = false;
        
        public override void OnFormInitialized()
        {
            try
            {
                base.OnFormInitialized();
                this.SharedLoad();
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
        }

        public override void OnFormReloaded()
        {
            try
            {   
                this.SharedLoad();
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
        }

        private void SharedLoad()
        {
            try
            {
                if (this.RecordId != null)
                {
                    if (String.Equals(TypeConvert.ToString(this.PrimaryDataRow["Type"]), "Rebate"))
                    {
                        Globals.EnableDisableSecondary(false, "RE_Promo_AddOn_Prod_Promotion_", false, false);
                        Globals.EnableDisableSecondaryFields(true, "RE_Promo_Mandatory_Prod_Promot", "Rebate_Amount");
                    }
                    else
                    {
                        Globals.EnableDisableSecondary(true, "RE_Promo_AddOn_Prod_Promotion_", true, true);
                        Globals.EnableDisableSecondaryFields(false, "RE_Promo_Mandatory_Prod_Promot", "Rebate_Amount");
                        Globals.EnableDisableSecondaryFields(false, "RE_Promo_AddOn_Prod_Promotion_", "Product_Id__RE_SKU_Description");
                        Globals.EnableDisableSecondaryFields(false, "RE_Promo_AddOn_Prod_Promotion_", "Unit_Price");
                    }
                }
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
        }

        public override bool SaveRecord()
        {
            try
            {
                DataTable addOnProducts = this.FormData.DataSet.Tables["RE_Promo_AddOn_Prod_Promotion_"].DefaultView.ToTable();
                DataTable mandatoryProducts = this.FormData.DataSet.Tables["RE_Promo_Mandatory_Prod_Promot"].DefaultView.ToTable();
                
                if (String.Equals(TypeConvert.ToString(this.PrimaryDataRow["Type"]), "Free") ||
                    String.Equals(TypeConvert.ToString(this.PrimaryDataRow["Type"]), "Top-Up") ||
                    String.Equals(TypeConvert.ToString(this.PrimaryDataRow["Type"]), "Purchase with Purchase"))
                {
                    if (mandatoryProducts != null && mandatoryProducts.Rows.Count > 0)
                    {
                        if (addOnProducts != null && addOnProducts.Rows.Count == 0)
                        {
                            PivotalMessageBox.Show("There should atleast one Add-On Product", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return false;
                        }
                    }
                    else
                    {
                        PivotalMessageBox.Show("Mandatory Product should be added", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return false;
                    }
                }
                return base.SaveRecord();
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc, true);
                return false;
            }
        }
        

        /// <summary>
        /// Disable a particular datagrid cell.
        /// </summary>
        /// <param name="secondaryName">Secondary Data Name</param>
        /// <param name="cellName">Cell name to be made read-only.</param>
        /// <param name="rowIndex">Row index for the cell.</param>
        /// <param name="enableCell">true, will enable the field for editing.</param>
        private static void EnableDisableDataGridCell(string secondaryName, string cellName, int rowIndex, bool enableCell)
        {
            IFormView formView = (IFormView)ClientContext.ClientAPIService.GetActiveContentObject();
            CdcSoftware.Pivotal.Engine.UI.Forms.PivotalClientFormControl formControl = (CdcSoftware.Pivotal.Engine.UI.Forms.PivotalClientFormControl)formView.FormControl;

            // Get the Secondary Segment into a PivotalSecondary object...
            CdcSoftware.Pivotal.Engine.UI.Forms.PivotalSecondary secondary = formControl.GetSecondary(secondaryName);

            if (secondary != null)
                secondary.Rows[rowIndex].Cells[cellName].ReadOnly = !enableCell;
        }
    }
}
